package so.wy.servicecenter.businessbed.service.device;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import so.wy.servicecenter.base.exception.ServiceException;
import so.wy.servicecenter.base.frame.baseopt.BaseOptService;
import so.wy.servicecenter.businessbed.dao.enums.StatusDevicePublishEnum;
import so.wy.servicecenter.businessbed.dao.enums.StatusDeviceRunEnum;
import so.wy.servicecenter.businessbed.dao.rdb.device.DeviceDAO;
import so.wy.servicecenter.businessbed.dao.rdb.device.DeviceDO;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital.HospitalDO;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital_building.HospitalBuildingDO;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital_depart.HospitalDepartDO;
import so.wy.servicecenter.businessbed.dao.rdb.order.order.OrderDO;
import so.wy.servicecenter.businessbed.manager.fm.HospitalFManager;
import so.wy.servicecenter.businessbed.manager.fm.OrderFManager;
import so.wy.servicecenter.businessbed.service.hospital.dto.HospitalPriceDTO;

@Service
@Scope("prototype")
public class DeviceService extends BaseOptService<DeviceDAO, DeviceDO, DeviceDTO> {

    @Autowired
    private HospitalFManager hospitalFManager;

    @Autowired
    private OrderFManager orderFManager;

    @Override
    protected DeviceDTO convert2DTO(DeviceDO deviceDO) {
        return new DeviceDTO(deviceDO);
    }

    @Override
    protected DeviceDO convert2DO(DeviceDTO deviceQuery) {
        return deviceQuery.convert2DO();
    }

    @Override
    public DeviceDTO addRecord(DeviceDTO recordQuery) throws ServiceException {
        //校验是否已经存在设备号
        if (count(lambdaQuery().eq(DeviceDO::getDeviceSerial, recordQuery.getDeviceSerial())) > 0) {
            throw exception("该设备号已存在！请勿重复添加!");
        }

        recordQuery.setDevicePublishStatus(StatusDevicePublishEnum.STOP.getCode());
        recordQuery.setDeviceStatus(StatusDeviceRunEnum.RENTABLE.getCode());

        return super.addRecord(recordQuery);
    }

    public void updateDeviceRunInfo(String deviceSerial, Double batteryBoard, Double batteryLock, String hardVer, String softVer) {
        DeviceDO upDO = new DeviceDO();
        upDO.setBatteryBoard(batteryBoard);
        upDO.setBatteryLock(batteryLock);
        upDO.setHardVer(hardVer);
        upDO.setSoftVer(softVer);

        update(upDO, new LambdaUpdateWrapper<DeviceDO>().eq(DeviceDO::getDeviceSerial, deviceSerial));
    }

    //通过设备编号获取设备信息
    public DeviceDetailsDTO getDeviceInfoBySerial(String deviceSerial) {
        DeviceDO deviceDO = getOne(lambdaQuery().eq(DeviceDO::getDeviceSerial, deviceSerial));

        DeviceDetailsDTO detailsDTO = new DeviceDetailsDTO(deviceDO);
        detailsDTO.setPriceSetting(new HospitalPriceDTO(hospitalFManager.getHospitalPriceInfo(deviceDO.getHospitalId())));

        return detailsDTO;
    }

    //设备投放
    public void publishDevice(Long deviceId, Long buildingId, Long departId) throws ServiceException {
        DeviceDO deviceDO = checkDeviceInfo(deviceId);

        if (deviceDO.getDevicePublishStatus().equals(StatusDevicePublishEnum.NORMAL.getCode())) {
            throw exception("该设备已投放，请先下架投放后，在进行绑定!");
        }

        HospitalBuildingDO buildingInfo = hospitalFManager.getHospitalBuildingInfo(buildingId);
        if (buildingInfo == null) {
            throw exception("投放地点有误");
        }

        HospitalDO hospitalInfo = hospitalFManager.getHospitalInfo(buildingInfo.getHospitalId());
        HospitalDepartDO hospitalDepart = hospitalFManager.getHospitalDepart(departId);

        //更新设备投放信息
        deviceDO.setDevicePublishStatus(StatusDevicePublishEnum.NORMAL.getCode());
        deviceDO.setHospitalId(buildingInfo.getHospitalId());
        deviceDO.setBuildingId(buildingInfo.getBuildingId());
        deviceDO.setFloorId(buildingInfo.getFloorId());
        deviceDO.setDepartId(departId);
        deviceDO.setHospitalName(hospitalInfo != null ? hospitalInfo.getHospitalName() : "");
        deviceDO.setBuildingName(buildingInfo.getBuildingName());
        deviceDO.setFloorName(buildingInfo.getFloorName());
        deviceDO.setDepartName(hospitalDepart != null ? hospitalDepart.getDepartName() : "");
        updateById(deviceDO);
    }

    //设备下架
    public void publishDownDevice(Long deviceId) throws ServiceException {
        DeviceDO deviceDO = checkDeviceInfo(deviceId);

//        //首先校验是否还有正在进行中的订单
//        OrderDO deviceRunOrder = orderFManager.getDeviceRunOrder(deviceDO.getDeviceSerial());
//        if (deviceRunOrder != null){
//            throw exception("该设备仍有订单在运行，请关闭订单后进行操作");
//        }

        //更新设备信息
        deviceDO.setDevicePublishStatus(StatusDevicePublishEnum.STOP.getCode());
        deviceDO.setHospitalId(null);
        deviceDO.setHospitalName(null);
        deviceDO.setBuildingId(null);
        deviceDO.setBuildingName(null);
        deviceDO.setFloorId(null);
        deviceDO.setFloorName(null);
        deviceDO.setDepartId(null);
        deviceDO.setDepartName(null);
        updateById(deviceDO);
    }

    private DeviceDO checkDeviceInfo(Long deviceId) throws ServiceException {
        DeviceDO deviceDO = getById(deviceId);
        if (deviceDO == null) {
            throw exception("设备信息不存在！");
        }
        return deviceDO;
    }

}
